<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/11/8
 * Time: 14:06
 */

namespace app\api\controller\v1;
use app\api\controller\BaseController;
use \app\api\service\Token;
use \app\api\service\Behavior as BehaviorService;
use app\api\validate\BehaviorValidate;
use think\Hook;

class Behavior extends BaseController
{
    /**
     * 前置操作
     * @var array
     */
    protected $beforeActionList = [
        'checkAuthorize' =>  ['except'=>'getBehavior,getThumbsUp'],
    ];

    /**
     * 获取行为
     * @param $father
     * @param $behavior
     * @param $details
     * @param $id
     * @return BehaviorService
     * @throws \think\Exception
     */
    public function getBehavior($father,$behavior,$details,$id){
      (new BehaviorValidate())->goCheck();
        $uid = Token::getCurrentUid();
        $user = explode('_',$father);
        $wu_id = $user[1];
        //行为监听
        if (in_array($behavior,['phone','coupon'])){
            if ($behavior == 'phone'){
                $weight = 6;
            }
            if ($behavior == 'coupon'){
                $weight = 7;
            }
            $param = ['wu_id'=>$wu_id,'user_id'=>$uid,'weight'=>$weight,'data'=>''];
            //行为监听
            Hook::exec('app\api\behavior\FollowBehavior','actionBegin',$param);
        }
        $service = new BehaviorService();
        $result =  $service->getBehavior($uid,$wu_id,$behavior,$details,$id);
        return $result;
    }

    /**
     * 获取用户点赞
     * @param $w_id
     * @return mixed
     */
    public function getThumbsUp($wu_id){
        $uid  = Token::getCurrentUid();
        $service = new BehaviorService();
        $result = $service->getThumbs($uid,$wu_id);
        return $result;
    }

    /**
     * 获取行为列表
     * @param $page
     * @param int $size
     * @return \think\response\Json
     */
    public function getBehaviorList($page,$size = 10){
        $uid = Token::getCurrentUid();
        $service = new BehaviorService();
        $result = $service->getBehaviorList($uid,$page,$size);
        if ($result != null){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取个人行为列表
     * @param $page
     * @param int $size
     * @return \think\response\Json
     */
    public function getBehaviorListByUser($uid,$page,$size = 10){
        $w_id = Token::getCurrentUid();
        $service = new BehaviorService();
        $result = $service->getBehaviorListByUser($w_id,$uid,$page,$size);
        if ($result != null){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取行为列表
     * @param $type
     * @param $timeInfo
     * @param $page
     * @param int $size
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getBehaviorListByCategory($type,$timeInfo,$page,$size = 5){
        $uid = Token::getCurrentUid();
        $service = new BehaviorService();
        $result = $service->getBehaviorListByCategory($type,$uid,$timeInfo,$page,$size);
        if ($result != null){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取各个行为统计数量
     * @param $type
     * @return \think\response\Json
     */
    public function getBehaviorNum($type,$timeInfo){
        $uid = Token::getCurrentUid();
        $service = new BehaviorService();
        $result =  $service->getCountInfo($uid,$type,$timeInfo);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取客户信息
     * @param $wu_id
     * @return mixed
     */
    public function getCustoms($timeInfo,$page,$size){
        $wu_id = Token::getCurrentUid();
        $service = new BehaviorService();
        $result = $service->getUser($wu_id,$timeInfo,$page,$size);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 根据用户id获取行为
     * @param $user_id
     * @return \think\response\Json
     */
    public function getCustomById($user_id,$timeInfo){
        $uid = Token::getCurrentUid();
        $service = new BehaviorService();
        $result = $service->getUserById($user_id,$timeInfo);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取行为统计数组
     * @return string
     */
    public function getBehaviorData($uid){
        $wu_id = Token::getCurrentUid();
        $service = new BehaviorService();
        $result =  $service->getBehaviorData($uid,$wu_id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 用户活跃度统计
     * @return string
     */
    public function getActiveData($uid,$time){
        $wu_id = Token::getCurrentUid();
        $service = new BehaviorService();
        $result =  $service->getActiveData($uid,$wu_id,$time);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 交互量统计
     * @param $uid
     * @return string
     */
    public function getInteractionData($uid){
        $wu_id = Token::getCurrentUid();
        $service = new BehaviorService();
        $result =  $service->getInteractionData($uid,$wu_id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取全部数据量统计
     * @return string
     */
    public function getAllBehaviorData(){
        $t_id = Token::getCurrentTokenVar('t_id');
        $service = new BehaviorService();
        $result =  $service->getAllBehaviorData($t_id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取用户总活跃度统计
     * @return string
     */
    public function getAllActiveData($time){
        $t_id = Token::getCurrentTokenVar('t_id');
        $service = new BehaviorService();
        $result =  $service->getAllActiveData($t_id,$time);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取总交互量统计
     * @param $uid
     * @return string
     */
    public function getAllInteractionData(){
        $t_id = Token::getCurrentTokenVar('t_id');
        $service = new BehaviorService();
        $result =  $service->getAllInteractionData($t_id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

}